<?php
class GoodsAttrControl extends AuthControl{
	// private $goods_type_gt_id;
	// //商品类型缓存
	// private $goodsTypeCache;
	public function __init(){
		$gtid  = Q("gtid",0,'intval');
		$GoodsTypeCache = cache('goodstype');
		if(!isset($GoodsTypeCache[$gtid])){
			$this->error('商品类型不存在');
		}
	}
	//显示属性列表
	public function index(){
		//商品类型ID
		$gtid = Q("gtid",0,'intval');
		if($gtid){
			//实例化一个扩展模型,此模型专门管理商品属性与属性值的
			$Model = K('AttrClass');
			$AttrClass = $Model->where(array('gtid'=>$gtid))->all();
			$this->assign('data',$AttrClass);
			$this->display();
		}else{
			$this->error('参数错误');
		}
	}
	//添加商品属性
	public function add(){

		if(IS_POST){
			$Model=K("AttrClass");
			if($Model->addAttr()){
				$this->success('添加成功..');
			}else{
				$this->error($Model->error); 
			}
		}else{
			$this->display();
		}
	}
	//添加属性时，根据选择的属性类型（SELECT,RADIO等）获得添加模型
	public function getShowTypeTpl(){
		//表单类型 如：INPUT TEXTAREA
		$show_type=Q('post.show_type');
		$Tpl = APP_PATH.'Field/'.$show_type.'/add.php';
		$content = $this->fetch($Tpl);
		echo $content;exit;
	}
	//删除
	public function delete(){
			$Model=K("AttrClass");
			if($Model->dorp()){
				$this->success('删除成功..');
			}else{
				$this->error($Model->error);  
			}
	}
	//编辑
	public function edit(){
		if(IS_POST){
			$Model=K("AttrClass");
			if($Model->editAttr()){
				$this->success('修改成功..');
			}else{
				$this->error($Model->error);
			}
		}else{
			$acid = Q('acid',0,'intval');
			if($acid){
				$Model = K("AttrClass");
				$AttrClassData = $Model->find($acid);
				//显示类型 box input
				$show_type = $AttrClassData['show_type'];
				$FieldTpl = APP_PATH.'Field/'.$show_type.'/edit.php';
				$AttrValueData = M("attr_value")->where("acid=".$AttrClassData['acid'])->all();
				//分配属性字段模型
				$this->assign("FieldTpl",$FieldTpl);
				$this->assign("AttrValueData",$AttrValueData);
				$this->assign("field",$AttrClassData);
				$this->display();
			}
		}
	}
}